package com.xbongbong.saas.model.impl;

import com.xbongbong.paas.domain.entity.PaasFormDataEntity;
import com.xbongbong.paas.elasticsearch.model.PaasEsModel;
import com.xbongbong.paas.elasticsearch.pojo.dto.DeleteDTO;
import com.xbongbong.paas.elasticsearch.pojo.dto.InsertDTO;
import com.xbongbong.paas.enums.IndexTypeEnum;
import com.xbongbong.paas.toolbox.exception.XbbException;
import com.xbongbong.paas.toolbox.util.BeanUtil;
import com.xbongbong.paas.toolbox.util.DateUtil;
import com.xbongbong.saas.domain.dao.ReimbursementFormDao;
import com.xbongbong.saas.domain.entity.ext.ReimbursementFormEntityExt;
import com.xbongbong.saas.model.ReimbursementFormModel;
import com.xbongbong.saas.util.StandardizeDataUtil;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;
import java.util.Map;
import java.util.Objects;

/**
 * 报销单
 * 创建时间 2020/3/14 4:49 PM
 * 修改时间 2020/3/14 4:49 PM
 * @author chy
 */
@Service("reimbursementFormModel")
public class ReimbursementFormModelImpl implements ReimbursementFormModel {

    @Resource
    private PaasEsModel paasEsModel;
    @Resource
    private ReimbursementFormDao reimbursementFormDao;


    @Override
    public Integer insert(ReimbursementFormEntityExt entity) throws XbbException {
        if (Objects.isNull(entity.getSerialNo())) {
            entity.setSerialNo("");
        }
        long now = DateUtil.getInt();
        entity.setAddTime(now);
        entity.setUpdateTime(now);
        StandardizeDataUtil.standardizeNotString(entity);
        Integer success = reimbursementFormDao.insert(entity);
        PaasFormDataEntity paasFormDataEntity = new PaasFormDataEntity();
        BeanUtil.copyProperties(entity, paasFormDataEntity);
        InsertDTO insertDTO = paasEsModel.getSaveDTO(paasFormDataEntity, IndexTypeEnum.IDX_SAAS_REIMBURSEMENT_FORM);
        paasEsModel.insert(insertDTO);
        return success;
    }

    @Override
    public Integer update(ReimbursementFormEntityExt entity) throws XbbException {
        long now = DateUtil.getInt();
        entity.setUpdateTime(now);
        StandardizeDataUtil.standardizeNotString(entity);
        Integer success = reimbursementFormDao.update(entity);
        PaasFormDataEntity paasFormDataEntity = new PaasFormDataEntity();
        BeanUtil.copyProperties(entity, paasFormDataEntity);
        paasFormDataEntity.setCreatorId(null);
        InsertDTO insertDTO = paasEsModel.getSaveDTO(paasFormDataEntity, IndexTypeEnum.IDX_SAAS_REIMBURSEMENT_FORM);
        paasEsModel.update(insertDTO);
        return success;
    }

    @Override
    public List<ReimbursementFormEntityExt> findEntitys(Map<String, Object> param) {
        return reimbursementFormDao.findEntitys(param);
    }

    @Override
    public ReimbursementFormEntityExt getByKey(Long key, String corpid) {
        return reimbursementFormDao.getByKey(key, corpid);
    }

    @Override
    public ReimbursementFormEntityExt getByFormDataCode(String corpid, String formDataCode) {
        return reimbursementFormDao.getByFormDataCode(corpid, formDataCode);
    }

    @Override
    public Integer getEntitysCount(Map<String, Object> param) {
        return reimbursementFormDao.getEntitysCount(param);
    }

    @Override
    public List findEntitysWithoutSub(Map<String, Object> param) {
        return null;
    }


    /**
     * 根据id删除联系人
     *
     * @param key    主键
     * @param corpid 公司id
     * @return java.lang.Integer
     * @author feng.zheng
     * @date 2019/2/22 20:23
     */
    @Override
    public Integer deleteByKey(Long key, String corpid) throws XbbException {
        Integer integer = reimbursementFormDao.deleteByKey(key, corpid);
        DeleteDTO deleteDTO = new DeleteDTO();
        deleteDTO.setEsId(corpid+"_"+ key);
        deleteDTO.setIndexTypeEnum(IndexTypeEnum.IDX_SAAS_REIMBURSEMENT_FORM);
        paasEsModel.delete(deleteDTO);
        return integer;
    }
}
